package com.example.sportplay.controller;

import com.alibaba.fastjson.JSON;
import com.example.sportplay.bean.*;
import com.example.sportplay.dao.MenuDao;
import com.example.sportplay.dao.UserDao;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@RestController
public class TestController {

    @Autowired
    private UserDao userDao;

    @CrossOrigin

    @RequestMapping("/txt")
    public String getUserList(QueryInfo queryInfo){
        int numbers = userDao.getUserCounts("%"+queryInfo.getQuery()+"%");// 获取数据总数
        int pageStart = (queryInfo.getPageNum()-1)*queryInfo.getPageSize();
        List<User> users = userDao.getAllUser("%"+queryInfo.getQuery()+"%",pageStart,queryInfo.getPageSize());
        HashMap<String, Object> res = new HashMap<>();
        res.put("numbers",numbers);
        res.put("data",users);
        String users_json = JSON.toJSONString(res);
        return users_json;
}
    @CrossOrigin

    @RequestMapping("/txtqian")
    public String getUserListqian(){

        List<User> users = userDao.getAllUserqiangg();
        HashMap<String, Object> res = new HashMap<>();

        res.put("data",users);

        String users_json = JSON.toJSONString(res);
        return users_json;
    }
    @CrossOrigin

    @RequestMapping("/findme")
    public String findme(String username){
        System.out.println(username);
        List<User> user= userDao.findme(username);
        HashMap<String, Object> res = new HashMap<>();

        System.out.println("res");
        res.put("data",user);


        String users_json = JSON.toJSONString(res);
        System.out.println(users_json);
        return users_json;
    }
    @CrossOrigin

    @RequestMapping("/findmebycarnumber")
    public String findmebycarnumber(String carnumber){

        List<User> user= userDao.findmebycarnumber(carnumber);
        HashMap<String, Object> res = new HashMap<>();

        System.out.println("通过车牌找人");
        res.put("data",user);


        String users_json = JSON.toJSONString(res);
        System.out.println(users_json);
        return users_json;
    }
    @CrossOrigin

    @RequestMapping("/findcarstate")
    public String findcarstate(String carnumber){
        System.out.println(carnumber);


        List user= userDao.findmestate(carnumber);
        HashMap<String, Object> res = new HashMap<>();


        res.put("data",user);

        System.out.println(user);


        String users_json = JSON.toJSONString(res);
        System.out.println(users_json);
        return users_json;
    }

    @CrossOrigin
    @RequestMapping("/deleteUser")
    public String deleteUser(String username){

        int i = userDao.deleteUser(username);
        String str = i >0?"success":"error";
        return str;
    }
    @CrossOrigin


    @RequestMapping("/addUser")
    public String addUser(@RequestBody User user){
        List<User> findmebycarnumber = userDao.findmebycarnumber(user.getCarnumber());
        System.out.println("注册");
        System.out.println(findmebycarnumber);
        if(findmebycarnumber.size()==0)
        {
            System.out.println(user);
            user.setRole("普通用户");
            user.setState(false);
            int i = userDao.addUser(user);
            String str = i >0?"success":"error";
            return str;
        }
        else{
            System.out.println("你已经注册，无须注册");
            String str="error";
            return str;

        }


//        System.out.println(findmebycarnumber);
//
//        User mmmm=new User();
//
//
//        mmmm=(User) findmebycarnumber.get(0);
//        String carnumber=mmmm.getCarnumber();
////        int i=1;
//        System.out.println(carnumber);
//        System.out.println(user.getCarnumber());
//
//        if(carnumber.equals(user.getCarnumber())){
//
//
//        }
//        else{
//
//
//        }
//

        //判断从车牌号是否在数据库中


    }
    @RequestMapping("/pdsf")
    public String findmestate(String carnumber){

        List findmestate = userDao.findmestate(carnumber);
        System.out.println(findmestate);
        User mmmm=new User();


        mmmm=(User) findmestate.get(0);
        String role=mmmm.getRole();
        int i=1;
        System.out.println(role);
        if(role.equals("普通用户"))
            i=0;
        System.out.println(i);

          String str = i !=0?"success":"error";
        System.out.println(str);
        System.out.println("str");
        return str;
    }



    @RequestMapping("/choosequanxian")
    public String choosequanxian(@RequestBody User user){

        user.setRole("超级管理员");
        user.setState(false);
        userDao.deleteUser(user.getUsername());
        int i = userDao.addUser(user);
        String str = i >0?"success":"error";
        return str;
    }
    @CrossOrigin

    @RequestMapping("/txtfind")
    public String getFindUserList(QueryInfo queryInfo){
        //  System.out.println(queryInfo);
//        int numbers = userDao.getUserCounts("%"+queryInfo.getQuery()+"%");// 获取数据总数
//        int pageStart = (queryInfo.getPageNum()-1)*queryInfo.getPageSize();
        List<User> users = userDao.getFindAllUser("%"+queryInfo.getQuery()+"%");
        HashMap<String, Object> res = new HashMap<>();
       // res.put("numbers",numbers);
        res.put("data",users);

        String users_json = JSON.toJSONString(res);
        // System.out.println("总条数："+users_json);
        return users_json;
    }
    @CrossOrigin
    @RequestMapping("/userState")
public String updateUserState(@RequestParam("username")String username, @RequestParam("state")Boolean state){

    int i = userDao.updateState(username, state);
  return i>0? "success":"error";

}

    @RequestMapping("/updaterole")
    public String updaterole(String carnumber){
        List user= userDao.findmebycarnumber(carnumber);
        User mmmm=new User();


        mmmm=(User) user.get(0);
        String username=mmmm.getUsername();
         String state="已缴费";
                 String statemoney="100";
        System.out.println(username);
        System.out.println(carnumber);
        System.out.println(state);
        System.out.println(statemoney);
        int insertmoney = userDao.insertmoney(username,carnumber,state,statemoney);
//        对money进行插入信息
        System.out.println(carnumber);

        int i = userDao.updaterole(carnumber);
        System.out.println(i);

        String str = i >0?"success":"error";
        System.out.println(str);
        return str;
    }


    @RequestMapping("/getUpdate")
    public String getUpdateUser(String username){

        User updateUser = userDao.getUpdateUser(username);
        String users_json = JSON.toJSONString(updateUser);
        return users_json;
    }

    @RequestMapping("/editUser")
    public String editUser(@RequestBody User user){


        int i = userDao.editUser(user);
        String str = i >0?"success":"error";
        return str;
    }
    @RequestMapping("/editUserqian")
    public String editUserqian(@RequestBody User user){
        System.out.println(user);
        int ii = userDao.editUserqianqian(user);
        int iii = userDao.editUserqianqianqian(user);
        int i = userDao.editUserqian(user);
        String str = i >0?"success":"error";
        System.out.println(str);
        return str;
    }

    @RequestMapping("/jiaofeimoney")
    public String jiaofeimoney(@RequestBody Money money){

        System.out.println("缴费的");
        money.setState("以缴费");

        System.out.println(money);
        String str="su";
//        int i = userDao.jiaofeimney(money);
//        String str = i >0?"success":"error";
//        System.out.println(str);
        return str;
    }
    @RequestMapping("/getuserbycarnumber")  //通过车牌找user
    public String getuserbycarnumber(String carnumber){
        System.out.println(carnumber);
        System.out.println("车牌号是这个");


        List user= userDao.findmebycarnumber(carnumber);
        HashMap<String, Object> res = new HashMap<>();


        res.put("data",user);

        System.out.println(user);
        User mmmm=new User();


        mmmm=(User) user.get(0);
        String role=mmmm.getRole();
        int i=1;
        System.out.println(role);
        if(role.equals("VIP"))
            i=0;
        System.out.println(i);

        String str = i !=0?"success":"error";
        System.out.println(str);
        System.out.println("str");
        return str;



    }
}
